//
// Created by Administrator on 2021/11/10.
//

#ifndef LEARNREALTIMERENDERING_COMMON_H
#define LEARNREALTIMERENDERING_COMMON_H

#include "glad/glad.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class GLException : std::exception
{
public:
    explicit GLException(const std::string& file, const std::string& function, int line)
    : m_Msg("Exception:" + file + "(" + function + ":" + std::to_string(line) + ")") {}
    const std::string& msg() const { return m_Msg; }

private:
    std::string m_Msg;
};

#define LOG(s) std::cout << (s) << std::endl
#define ERROR(s) std::cerr << (s) << std::endl
#define ThrowIfFailed(x) \
    if(!(x)) throw GLException(__FILE__, __FUNCTION__, __LINE__)

static void LoadFile(const std::string& file, std::string& content)
{
    std::ifstream ifs;
    std::stringstream ss;
    ifs.exceptions(std::ios::failbit | std::ios::badbit);
    try
    {
        ifs.open(file);
        ss << ifs.rdbuf();
        ifs.close();
        content = ss.str();
    }
    catch(const std::ifstream::failure& e)
    {
        ERROR(e.what());
    }
}

#endif //LEARNREALTIMERENDERING_COMMON_H
